package cn.sinata.xldutils_kotlin.activity

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.content.pm.ActivityInfo
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.text.TextUtils
import android.view.Window
import android.view.WindowManager
import cn.sinata.xldutils_kotlin.R
import cn.sinata.xldutils_kotlin.utils.SPUtils
import cn.sinata.xldutils_kotlin.widget.ProgressDialog
import io.reactivex.disposables.CompositeDisposable
import org.jetbrains.anko.AnkoLogger
import java.lang.Exception

/**
 * 基础activity，包含设置默认强制竖屏显示，广播方式实现关闭全部继承自该activity，并注册了关闭广播的子类
 *
 */
abstract class BaseActivity : AppCompatActivity(), AnkoLogger {

    lateinit var ACTION_CLOSE_ALL: String
    val compositeDisposable = CompositeDisposable()
    //改用lazy初始，第一次使用时才会初始化
    private val dialog: ProgressDialog by lazy {
        ProgressDialog(this, R.style.Theme_ProgressDialog)
    }

    var isDestroy = false

    private val closeAllReceiver = object : BroadcastReceiver() {
        override fun onReceive(context: Context?, intent: Intent?) {
            if (intent != null && TextUtils.equals(intent.action, ACTION_CLOSE_ALL)) {
                finish()
            }
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        requestWindowFeature(Window.FEATURE_NO_TITLE)
        //竖屏
        requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
        ACTION_CLOSE_ALL = "cn.sinata.base.%s.all.close".format(packageName)
        System.err.println(ACTION_CLOSE_ALL)
        if (isRegisterCloseBroadReceiver()) {
            registerReceiver(closeAllReceiver, IntentFilter(ACTION_CLOSE_ALL))
        }
    }

    override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)
        if (permissions.isNotEmpty()) {
            for (permission in permissions) {
                SPUtils.instance().put(permission, false).apply()
            }
        }
    }

    /**
     * 是否注册关闭全部的广播
     */
    protected fun isRegisterCloseBroadReceiver(): Boolean {
        return true
    }

    override fun onDestroy() {
        isDestroy = true
        super.onDestroy()
        if (!compositeDisposable.isDisposed) {
            compositeDisposable.dispose()
        }

        try {
            if (isRegisterCloseBroadReceiver()) {
                unregisterReceiver(closeAllReceiver)
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }
        dismissDialog()
        //隐藏键盘
        window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
    }

    fun showDialog(msg: String = "加载中...", canCancel: Boolean = true) {
        if (!canCancel) {
            dialog.setOnCancelListener {
                if (this.finishWhenCancelDialog()) {
                    finish()
                }
            }
        } else {
            //这里设置如果是可以取消的监听器置null了。可以自己在页面上重新设置想要的操作。这里不知道具体需求。
            dialog.setOnCancelListener(null)
        }
        dialog.setCanceledOnTouchOutside(canCancel)
        dialog.setMessage(msg)
        if (!dialog.isShowing) {
            dialog.show()
        }
    }

    fun dismissDialog() {
        if (dialog.isShowing) {
            dialog.dismiss()
        }
    }

    /**
     * 是否在取消progressDialog的时候关闭页面
     */
    protected open fun finishWhenCancelDialog() = true
}